//
//  ViewController.m
//  MapDemo
//
//  Created by JOKER on 16/11/11.
//  Copyright © 2016年 JOKER. All rights reserved.
//  

#import "ViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Search/BMKPoiSearch.h>

NSString *const resultCellID = @"resultCellID";

@interface ViewController ()<BMKLocationServiceDelegate, UITableViewDataSource, UITableViewDelegate, BMKPoiSearchDelegate, BMKMapViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (nonatomic, weak) BMKMapView *mapView;
@property (nonatomic, strong) BMKLocationService *locService;

/** 用来展示附近地点的tableView */
@property (weak, nonatomic) IBOutlet UITableView *resultDisplayView;
/** 目标位置 */
@property (nonatomic, strong) BMKPointAnnotation *destinationPoint;
/** 检索结果 */
@property (nonatomic, strong) NSArray<BMKPoiInfo *> *poiInfoArray;

@property (nonatomic, weak) UIButton *preClickedTabBtn;

@property (nonatomic, strong) BMKPoiSearch *poiSearch;
@end

@implementation ViewController

#pragma mark - 懒加载
- (BMKLocationService *)locService {
    if (!_locService) {
        _locService = [[BMKLocationService alloc] init];
        
        _locService.distanceFilter = 100;
        [_locService startUserLocationService];
    }
    return _locService;
}

- (BMKPoiSearch *)poiSearch {
    if (!_poiSearch) {
        BMKPoiSearch *poiSearch = [[BMKPoiSearch alloc] init];
        _poiSearch = poiSearch;
    }
    return _poiSearch;
}

- (BMKPointAnnotation *)destinationPoint {
    if (!_destinationPoint) {
        BMKPointAnnotation *destinationPoint = [[BMKPointAnnotation alloc] init];
        _destinationPoint = destinationPoint;
    }
    return _destinationPoint;
}

- (NSArray<BMKPoiInfo *> *)poiInfoArray {
    if (!_poiInfoArray) {
        _poiInfoArray = [[NSArray alloc] init];
    }
    return _poiInfoArray;
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.resultDisplayView registerClass:[UITableViewCell class] forCellReuseIdentifier:resultCellID];
    
    [self locService];
    
    BMKMapView *mapView = [[BMKMapView alloc] init];
    mapView.frame = self.topView.bounds;
    [self.topView addSubview:mapView];
    self.mapView = mapView;
    
    mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
    mapView.showMapScaleBar = YES;
    mapView.showsUserLocation = YES;
    
    [mapView setZoomLevel:18];
}

- (void)viewWillAppear:(BOOL)animated {
    self.mapView.delegate = self;
    self.poiSearch.delegate = self;
    self.locService.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated {
    self.mapView.delegate = nil;
    self.poiSearch.delegate = nil;
    self.locService.delegate = nil;
}

#pragma mark - custom method
/** 搜索周边 */
- (void)searchNearbyWithKeyword:(NSString *)keyword {
    BMKNearbySearchOption *searchOption = [[BMKNearbySearchOption alloc] init];
    // 搜索关键字
    searchOption.keyword = keyword;
    // 搜索中心
    searchOption.location = self.destinationPoint.coordinate;
    // 搜索半径
    searchOption.radius = 1000;
    [self.poiSearch poiSearchNearBy:searchOption];
}

- (IBAction)tabBtnClicked:(UIButton *)sender {
    self.preClickedTabBtn.selected = NO;
    sender.selected = YES;
    self.preClickedTabBtn = sender;
    
    NSString *keyword = sender.titleLabel.text;
    if ([keyword isEqualToString:@"全部"]) keyword = @" ";
    
    [self searchNearbyWithKeyword:keyword];
}

#pragma mark - BMKLocationServiceDelegate
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    
    // 使当前位置在地图中央
    [self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    // 标注
//    BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc]init];
//    pointAnnotation.coordinate = userLocation.location.coordinate;
//    [self.mapView addAnnotation:pointAnnotation];
    
//    self.destinationPoint = pointAnnotation;
}


#pragma mark - tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.poiInfoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resultCellID];
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:resultCellID];
    BMKPoiInfo *poiInfo = self.poiInfoArray[indexPath.row];
    cell.textLabel.text = poiInfo.name;
    cell.detailTextLabel.text = poiInfo.address;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    
    return cell;
}

#pragma mark - BMKMapViewDelegate
- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate {
//- (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate {
    // 移除上一个标注点
    [self.mapView removeAnnotation:self.destinationPoint];
    self.destinationPoint = nil;
    
    // 添加新标注点
    self.destinationPoint.coordinate = coordinate;
    [self.mapView addAnnotation:self.destinationPoint];
    
    [self searchNearbyWithKeyword:@" "];
}

- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    NSLog(@"regionDidChangeAnimated");
}

- (void)mapViewDidFinishRendering:(BMKMapView *)mapView {
    NSLog(@"mapViewDidFinishRendering");
//    mapView.centerCoordinate
}

#pragma mark - BMKPoiSearchDelegate
/** 获取 poi 搜索结果 */
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode {
    if (errorCode) {
        NSLog(@"搜索失败");
    }
    self.poiInfoArray = poiResult.poiInfoList;
    [self.resultDisplayView reloadData];
}








@end
